package com.yan.demo2;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;

/**
 * @author taoge
 * @Date 2024/8/14 11:10
 * @Description
 */
public class Test2 {
    @Test
    public void test2() throws Exception {
        Class<Book> bookClass = Book.class;
        Field author = bookClass.getDeclaredField("author");
        System.out.println(author.getModifiers());
        String name = author.getName();
        System.out.println(name);
        Book book = new Book("JAVA" , "yft" , 19.9 , new Date());
        author.setAccessible(true);
        String o = (String) author.get(book);
        System.out.println(o);
        author.set(book , "wl");
        System.out.println(author.get(book));

    }

    @Test
    public void test3() throws Exception {
        Class<Book> bookClass = Book.class;
        Method setName = bookClass.getDeclaredMethod("setName", String.class);
        System.out.println(setName.getName());
        int modifiers = setName.getModifiers();
        System.out.println("modifiers = " + modifiers);
        Class returnType = setName.getReturnType();
        String simpleName = returnType.getSimpleName();
        System.out.println("simpleName = " + simpleName);
        Class[] parameterTypes = setName.getParameterTypes();
        for (Class parameterType : parameterTypes) {
            System.out.println(parameterType);
        }

        setName.setAccessible(true);
        Book book = new Book("java", "yft", 29.9, new Date());
        System.out.println(setName.invoke(book, "yft"));

    }

    @Test
    public void test4() throws Exception {
        Class bookClass = Book.class;
        Constructor cons = bookClass.getConstructor(String.class , String.class , Double.class , Date.class);
        System.out.println(cons.getModifiers());
        System.out.println(Arrays.toString(cons.getParameterTypes()));
        System.out.println(cons.getName());

        cons.setAccessible(true);

    }
}
